////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft Corporation.  All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
// Portions Copyright (c) 2012 Kentaro Sekimoto  All rights reserved.
////////////////////////////////////////////////////////////////////////////

#include <tinyhal.h>
#include "..\FM3.h"
#include "FM3_I2C_driver.h"

#define CQ_I2C_CH	1	// CQ_FRK_FM3 board has FRAM with I2C channel 1.
#define DEF_I2C_CH	7

UINT32 i2c_ch = DEF_I2C_CH;

BOOL I2C_Internal_Initialize()
{
    BOOL flag = TRUE;
    flag |= FM3_I2C_Driver::Initialize(DEF_I2C_CH);
#if defined(PLATFORM_ARM_CQ_FRK_FM3) || defined(PLATFORM_ARM_CQFM3DUINO) || \
    defined(PLATFORM_ARM_WKFM3) || defined(PLATFORM_ARM_WXMP3PLCD_FM3)
    flag |= FM3_I2C_Driver::Initialize(CQ_I2C_CH);
#endif
    return flag;
}

BOOL I2C_Internal_Uninitialize()
{
    BOOL flag = TRUE;
    flag |= FM3_I2C_Driver::Uninitialize(DEF_I2C_CH);
#if defined(PLATFORM_ARM_CQ_FRK_FM3) || defined(PLATFORM_ARM_CQFM3DUINO) || \
    defined(PLATFORM_ARM_WKFM3) || defined(PLATFORM_ARM_WXMP3PLCD_FM3)
    flag |= FM3_I2C_Driver::Uninitialize(CQ_I2C_CH);
#endif
    return flag;
}

void I2C_Internal_XActionStart(I2C_HAL_XACTION* xAction, bool repeatedStart)
{
    FM3_I2C_Driver::MasterXAction_Start(i2c_ch, xAction, repeatedStart);
}

void I2C_Internal_XActionStop()
{
    FM3_I2C_Driver::MasterXAction_Stop(i2c_ch);
}

void I2C_Internal_GetClockRate(UINT32 rateKhz, UINT8& clockRate, UINT8& clockRate2)
{
    FM3_I2C_Driver::GetClockRate(i2c_ch, rateKhz, clockRate, clockRate2);
}

void I2C_Internal_GetPins(GPIO_PIN& scl, GPIO_PIN& sda)
{
    FM3_I2C_Driver::GetPins(i2c_ch, scl, sda);
}

void I2C_Internal_SetChannel(INT32 channel)
{
    i2c_ch = channel;
    //lcd_printf("I2C ch=%d\r\n", channel);
}
